package com.dzzw.Dao;

import com.dzzw.Model.Detail;
import com.dzzw.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//投票明细
public class DetailDao {
    private final Connection con = DButils.getCon();
    //增加投票明细信息
    public void DetailAdd(Detail detail){
        Date detail_date = new Date();
        //设置日期格式化样式为：yyyy-MM-dd
        SimpleDateFormat SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String sql = "insert into t_detail(vote_id,option_id,user_id,detail_date) values(?,?,?,?)";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,detail.getVote_id());
            preparedStatement.setInt(2,detail.getOption_id());
            preparedStatement.setInt(3,detail.getUser_id());
            preparedStatement.setDate(4, java.sql.Date.valueOf(SimpleDateFormat.format(detail_date.getTime())));//获取时间
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            System.out.println("数据添加失败！");

            e.printStackTrace();
        }
    }

    //删除投票明细（撤回投票）

    //查看投票明细信息
    public static List<Detail> SelectAllDetail(){
        List<Detail> list = new ArrayList<>();
        String sql = "SELECT d.detail_id,v.vote_title,v.vote_introduce,o.option_title,u.user_name FROM t_user AS u JOIN t_detail AS d ON d.user_id = u.user_id INNER JOIN t_option AS o ON d.option_id = o.option_id INNER JOIN t_vote AS v ON d.vote_id = v.vote_id AND o.vote_id = v.vote_id ORDER BY d.detail_id ASC";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Detail detail = new Detail();
                detail.setDetail_id(resultSet.getInt(1));
                detail.setVote_title(resultSet.getString(2));
                detail.setVote_introduce(resultSet.getString(3));
                detail.setOption_title(resultSet.getString(4));
                detail.setUser_name(resultSet.getString(5));
                list.add(detail);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    //根据用户id查询投票明细
    public static List<Detail> SelectAllDetailByUID(int user_id){
        List<Detail> list = new ArrayList<>();
        String sql = "SELECT d.detail_id,v.vote_title,v.vote_introduce,o.option_title,u.user_name FROM t_user AS u JOIN t_detail AS d ON d.user_id = u.user_id INNER JOIN t_option AS o ON d.option_id = o.option_id INNER JOIN t_vote AS v ON d.vote_id = v.vote_id AND o.vote_id = v.vote_id where d.user_id = ? ORDER BY d.detail_id ASC";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,user_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Detail detail = new Detail();
                detail.setDetail_id(resultSet.getInt(1));
                detail.setVote_title(resultSet.getString(2));
                detail.setVote_introduce(resultSet.getString(3));
                detail.setOption_title(resultSet.getString(4));
                detail.setUser_name(resultSet.getString(5));
                list.add(detail);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
